package edu.bw.web.user;

import cn.hutool.json.JSONUtil;
import edu.bw.common.CommonResult;
import edu.bw.dto.UpdateUserForm;
import edu.bw.service.UserService;
import edu.bw.service.impl.UserServiceImpl;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

@WebServlet("/user/update")
public class UpdateUserServlet extends HttpServlet {
    UserService userService = new UserServiceImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      //{"userId":29,"username":"zhangsan12345","password":"7899990qw","name":"张三","sex":"男","tel":"17865437882"
        // ,"email":"17865437882@qq.com","hiredate":"2024-08-08","role":[4,3,2],"deptId":4,"status":1}
        String reqJSON = request.getReader().readLine();
        UpdateUserForm updateUserForm = JSONUtil.toBean(reqJSON, UpdateUserForm.class);

        Integer rows = userService.update(updateUserForm);

        response.getWriter().write(JSONUtil.toJsonStr(CommonResult.ok().put("rows", rows)));

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}